<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form :model="searchInfo" :inline="true" size="small">
        <el-form-item label="上报时间">
          <el-date-picker
            v-model="timeValue"
            style="width: 300px;"
            clearable
            size="small"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            align="right"
          />
        </el-form-item>
        <el-form-item label="事件分类">
          <el-select v-model="searchInfo.eventNo" clearable placeholder="请选择">
            <el-option v-for="item in eventClassify" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="严重程度">
          <el-select v-model="searchInfo.eventLevel" clearable placeholder="请选择">
            <el-option v-for="item in severityList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="流转状态">
          <el-select v-model="searchInfo.status" clearable placeholder="请选择">
            <el-option v-for="item in circulationStatusList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="onSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table ref="tableRef" v-loading="loading" :data="ticketList" stripe>
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column label="不安全事件分类" prop="eventName" align="center" :show-overflow-tooltip="true" />
        <el-table-column label="上报日期" align="center" prop="createTime" width="180" />
        <el-table-column label="严重程度" prop="eventLevel" :show-overflow-tooltip="true" width="150" align="center" />
        <el-table-column label="流转状态" prop="statusName" :show-overflow-tooltip="true" width="150" align="center" />

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-document" @click="onLook(scope.row)">查看</el-button>
            <el-button size="mini" style="color: #63A103;" type="text" @click="onHistory(scope.row)">审批历史</el-button>
            <el-button size="mini" style="color: #910012;" type="text" @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageInfo.total > 0" :total="pageInfo.total" :page.sync="pageInfo.currentPage" :limit.sync="pageInfo.pageSize" @pagination="onPagination" />
    </el-card>

    <HistoryDialog ref="historyRef" />

  </div>
</template>

<script>
import { getCompletedList, doDelete } from '@/api/newBaqsj/newBaqsj'
import { eventClassify, circulationStatusList, processingStatus } from '../common'
import HistoryDialog from '../components/history'
export default {
  name: 'Ended',
  components: {
    HistoryDialog
  },
  data() {
    return {
      eventClassify, // 事件分类
      processingStatus, // 处理状态
      circulationStatusList, // 流转状态列表
      searchInfo: {
        eventNo: '', // 事件分类
        auditStatus: null, // 处理状态
        status: null, // 流转状态
        startOpenTime: null, // 申报开始时间
        endOpenTime: null // 申报结束时间
      },
      searchInfoCopy: null, // 用于重置
      pageInfo: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      timeValue: [],
      loading: false, // loading加载
      ticketList: [], // 列表数据
      selectIds: '', // 选中的id
      selectedRow: null,
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  watch: {
    // 处理申报时间
    timeValue: {
      handler(newVal, oldVal) {
        if (newVal && newVal.length) {
          this.searchInfo.startOpenTime = newVal[0]
          this.searchInfo.endOpenTime = newVal[1]
        } else {
          this.searchInfo.startOpenTime = null
          this.searchInfo.endOpenTime = null
        }
      },
      deep: true
    }
  },
  created() {
    this.searchInfoCopy = JSON.parse(JSON.stringify(this.searchInfo))
    this.getList()
  },
  methods: {
    // 查看详情
    onLook(row) {
      // this.$refs.detailRef.open(row)
      this.$router.push({ path: '/newBaqsj/lookDetail', query: { id: row.id }})
    },
    // 进度
    onHistory(row) {
      this.$refs.historyRef.open(row)
    },
    // 删除数据
    onDelete(row) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        doDelete({ id: row.id }).then(response => {
          this.$message.success('删除成功!')
          this.onSearch()
        })
      })
    },
    // 搜索
    onSearch() {
      this.pageInfo.currentPage = 1
      this.getList()
    },
    // 重置
    resetQuery() {
      this.timeValue = []
      this.searchInfo = JSON.parse(JSON.stringify(this.searchInfoCopy))
      this.getList()
    },
    // 分页
    onPagination(obj) {
      this.pageInfo.currentPage = obj.page
      this.pageInfo.pageSize = obj.limit
      this.getList()
    },
    // 列表数据
    getList() {
      const params = {
        ...this.searchInfo,
        ...this.pageInfo
      }
      getCompletedList(params).then(res => {
        const data = res.records
        data.forEach(item => {
          const statusObj = this.circulationStatusList.find(it => it.value === item.status)
          const auditStatusObj = this.processingStatus.find(it => it.value === item.auditStatus)
          item.statusName = statusObj ? statusObj.label : ''
          item.auditStatusName = auditStatusObj ? auditStatusObj.label : ''
        })
        this.ticketList = data
        this.pageInfo.total = res.total
      })
    }
  }
}
</script>

<style scoped></style>
